#!/bin/bash

cluster_hosts="master.hadoop slave1.hadoop slave2.hadoop"

echo "config ssh public key login"
for host in $cluster_hosts; do
	ssh-keyscan -H $host >> ~/.ssh/known_hosts
	expect ./ssh_public_key.exp $host
done

echo "config sudo nopasswd"
for host in $cluster_hosts; do
	expect ./ssh_sudo_nopasswd.exp $host
done

echo "copy hosts file"
for host in $cluster_hosts; do
	cat ./hosts | ssh $host "sudo tee /etc/hosts 1>/dev/null"
done

echo "set hostname"
for host in $cluster_hosts; do
	hname=$(grep $host ./hosts | cut -f2)
	ssh $host "sudo sed -i '1s/.*/${hname}/' /etc/hostname && sudo hostname $hname"
done

echo "generate ssh key and copy id to others"
for host in $cluster_hosts; do
	scp ./gen_copy_key.sh $host:~/
	scp ./ssh_public_key.exp $host:~/
	others=$(echo -n $cluster_hosts | sed "s/$host//")
	echo "others is: $others"
	ssh $host "./gen_copy_key.sh $others"
done
